package io;

import java.io.*;
import java.util.Scanner;

/*
* 文件复制
* */
public class FileDemo04 {
    public static void main(String[] args) throws IOException {
        //1.输入复制的路径和目标路径
        Scanner in =new Scanner(System.in);
        System.out.println("请输入须要复制的文件路径:");
        String srcpath=in.next();
        System.out.println("请输入复制的目标路径:");
        String detinpath=in.next();
        //2.验证路径的合法性
        File srcfile=new File(srcpath);
        if(!srcfile.isFile()){
            System.out.println("文件路径不合法");
            return;
        }
        File detinfile=new File(detinpath);
        if(!detinfile.getParentFile().isDirectory()){
            System.out.println("文件目标路径不合法");
            return;
        }
        //3.执行复制操作
        byte[] bytes=new byte[1024];
        try(InputStream inputStream=new FileInputStream(srcfile);
        OutputStream outputStream=new FileOutputStream(detinfile)){
            while(true){
                int n=inputStream.read(bytes);
                if(n==-1){
                    break;
                }
                outputStream.write(bytes,0,n);

            }
        }
    }
}
